﻿using System;
using System.Windows.Data;

namespace Phone7ActionPack.ValueConverters
{
    /// <summary>
    /// this will take dates and tell you how long ago it was in human terms
    /// </summary>
    public class TimeAgoValueConverter : IValueConverter 
    {
        const string FORMAT = "{0} {1}s";
        private enum InputTypes { DateTime, StringDateFormat,DateTimeOffset }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            
            InputTypes type = InputTypes.StringDateFormat;

            DateTime inputDateTime = DateTime.Now;
            Type t = value.GetType();
            if (t== typeof(DateTime))
                type = InputTypes.DateTime;
            if (t == typeof(DateTimeOffset))
                type = InputTypes.DateTimeOffset;
            switch (type)
            {
                case InputTypes.DateTime:
                    inputDateTime = (DateTime)value;
                    break;
                case InputTypes.StringDateFormat:
                    if (!DateTime.TryParse(value.ToString(), out inputDateTime))
                        return "In the past"; // we cant figure out how to show this 
                    break;
                case InputTypes.DateTimeOffset:
                    inputDateTime = ((DateTimeOffset)value).LocalDateTime;
                    break;
                default:
                    return "In the past";// we cant figure out how to show this 
            }
            var currentOffset = DateTime.Now;
            string humanString = "";
            TimeSpan span = currentOffset.Subtract(inputDateTime);
            if (span.Days < 1) // less than 1 day
            {
                if (span.Hours < 1) // x Minutes
                    humanString = string.Format(FORMAT, span.Minutes, "Minute");
                else // x Hours
                    humanString = string.Format(FORMAT, span.Hours, "Hour");
            }
            else // more than 1 day
            {
                if (span.Days < 7)
                    humanString = string.Format(FORMAT, span.Days, "Day");
                else
                {
                    int months = (int)Math.Floor(span.Days / 30);
                    int weeks = (int)Math.Floor(span.Days / 7);
                    int years = (int)Math.Floor(span.Days / 365);
                    if (years > 0)
                        humanString = string.Format(FORMAT, years, "Year");
                    else 
                        if(months > 0)
                            humanString = string.Format(FORMAT, months, "Month");
                        else
                            humanString = string.Format(FORMAT, weeks, "Week");
                }

            }
            if (humanString.StartsWith("1"))
                humanString = humanString.TrimEnd('s');

            return humanString + " Ago";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}